/*
 * Copyright (C) 2013 The original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy
 * of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

package com.zapta.apps.maniana.editors;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.ToggleButton;
import android.widget.Spinner;

import com.zapta.apps.maniana.R;
import com.zapta.apps.maniana.annotations.MainActivityScope;
import com.zapta.apps.maniana.model.ItemColor;
import com.zapta.apps.maniana.util.LogUtil;

/*
**
** @author Elliott Mitchell
**/
@MainActivityScope
public class ItemTextEditorExtras extends ItemTextEditorExtra
implements CompoundButton.OnCheckedChangeListener {

	@Override
	public View onCreateView(LayoutInflater inflator, ViewGroup container,
Bundle savedInstanceState) {
		super.onCreateView(inflator, container, savedInstanceState);
		final View view=inflator.inflate(R.layout.editor_extras,
container, false);

		Spinner colors=(Spinner)view.findViewById(R.id.editor_color);
		colors.setAdapter(mColors);
		colors.setOnItemSelectedListener(this);

		if(getArguments().containsKey("item_color")) {
			colors.setSelection(ItemColor.fromKey(getArguments().
getString("item_color"), null).getIndex());
		}

		getArguments().remove("item_color_early");
		getArguments().remove("item_color_late");

		final int dueDate=getArguments().getInt("item_due_date", 0);
		final ToggleButton dayBut=(ToggleButton)view.findViewById(R.id.
editor_day);
		final CheckBox lockBut=(CheckBox)view.findViewById(R.id.
editor_lock);

		switch(dueDate) {
		case 0: // today
			lockBut.setEnabled(false);
		break;
		case -1: // tomorrow
			dayBut.setChecked(true);
		break;
		case -2: // tomorrow+locked
			dayBut.setChecked(true);
			lockBut.setChecked(true);
		break;
		default:
			LogUtil.error(dueDate>0?"EditorExtras: loaded on "+
"item with due date":"EditorExtras: unknown item_due_date value");
		}
		dayBut.setOnCheckedChangeListener(this);
		lockBut.setOnCheckedChangeListener(this);

		return view;
	}

	public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
		getArguments().putString("item_color", ItemColor.values()[pos].getKey());
	}

	int getSpinnerTextId(int id) {
		return R.string.editor_color;
	}

	public void onCheckedChanged(CompoundButton but, boolean st) {
		final int dueDate;
		switch(but.getId()) {
		case R.id.editor_day:
			final CheckBox lock=(CheckBox)getView().findViewById(R.
id.editor_lock);
			lock.setEnabled(st);
			if(st) dueDate=-1;
			else {
				dueDate=0;
				lock.setChecked(false);
			}
		break;
		case R.id.editor_lock:
			dueDate=st?-2:-1;
		break;
		default:
			return;
		}
		getArguments().putInt("item_due_date", dueDate);
	}
}

